﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;
using System.Xml.Serialization;
using System.Net;
using System.Net.Sockets;
using WhiteBoard;
using LumiSoft.Net.UDP;
using LumiSoft.Net.Codec;
using WinSound.Media.Wave;

namespace WFFS
{
    public partial class FrmSound : Form
    {        
        #region 常用对象
        private Form1 frmMain = null;
        System.Windows.Forms.Timer m_TimerDrawProgressBar;
        #endregion

        public FrmSound(Form1 _frm)
        {
            InitializeComponent();
            this.frmMain = _frm;
            this.frmMain.frmSound = this;
            //frmMain.aduio_netWork.frm_Sound = this;            
            InitSound();
            InitUdpCall();
        }

        private void InitSound()
        {
            string sqlCenter = "select ipaddr,isLeader from UserInfo where rfid='0000' ";
            SqlCommand cmdCenter = new SqlCommand(sqlCenter, frmMain.dbConnect);
            SqlDataAdapter datapterCenter = new SqlDataAdapter(cmdCenter);
            DataTable dtCenter = new DataTable();
            datapterCenter.Fill(dtCenter);
            if (dtCenter.Rows.Count > 0)
            {
                linkCenter.LinkColor = Color.Black;
                linkCenter.Tag = dtCenter.Rows[0]["isLeader"].ToString() + "|" + dtCenter.Rows[0]["ipaddr"].ToString();
                linkCenter.Click += new EventHandler(BtnPerToPer_Click);
            }
            dtCenter.Dispose();
            dtCenter = null;
            datapterCenter.Dispose();
            datapterCenter = null;
            cmdCenter.Dispose();
            cmdCenter = null;

            string sql = "select distinct Section from UserInfo where Section <> '' and isHasPad=1 order by Section desc";
        
            SqlCommand cmd1 = new SqlCommand(sql, frmMain.dbConnect);
            SqlDataAdapter datapter = new SqlDataAdapter(cmd1);
            DataSet ds = new DataSet();
            datapter.Fill(ds);

            if (ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TableLayoutPanel tabTmp = new TableLayoutPanel();
                    tabTmp.ColumnCount = 3;
                    tabTmp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 2F));
                    tabTmp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20F));
                    tabTmp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 88F));
                    tabTmp.Dock = DockStyle.Top;
                    tabTmp.Height = 60;

                    tabTmp.BackColor = new Color();
                    //tabPanelMain.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
                    //tabPanelMain.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    PictureBox picBox = new PictureBox();
                    Size m = new System.Drawing.Size();
                    m.Width = 40;
                    m.Height = 40;
                    picBox.Size = m;
                    switch (ds.Tables[0].Rows[i][0].ToString())
                    {
                        case "1":
                            picBox.BackgroundImage =WFFSClients.Properties.Resources.S1;
                           
                            break;
                        case "2":
                            picBox.BackgroundImage = WFFSClients.Properties.Resources.S2;
                            break;
                        case "3":
                            picBox.BackgroundImage = WFFSClients.Properties.Resources.S3;
                            break;
                        case "4":
                            picBox.BackgroundImage = WFFSClients.Properties.Resources.S4;
                            break;
                        case "5":
                            picBox.BackgroundImage = WFFSClients.Properties.Resources.S5;
                            break;
                        case "6":
                            picBox.BackgroundImage = WFFSClients.Properties.Resources.S6;
                            break;
                        case "7":
                            picBox.BackgroundImage = WFFSClients.Properties.Resources.S7;
                            break;
                        case "8":
                            picBox.BackgroundImage = WFFSClients.Properties.Resources.S8;
                            break;
                        case "9":
                            picBox.BackgroundImage = WFFSClients.Properties.Resources.S9;
                            break;
                    }
                    
                    tabTmp.Controls.Add(picBox, 1, i);



                    TableLayoutPanel tabTmp01 = new TableLayoutPanel();
                    tabTmp01.ColumnCount = 3;
                    //tabTmp01.BackColor = System.Drawing.Color.DarkBlue;
                    Size m01 = new System.Drawing.Size();
                    m01.Width = 250;
                    m01.Height = 60;

                    tabTmp01.Size = m01;

                    sql = "select * from UserInfo where Section = " + ds.Tables[0].Rows[i][0].ToString() + " and isHasPad = 1 and ipaddr<>'" + Form1.Audio_serverIP + "'";
                    SqlCommand cmd01 = new SqlCommand(sql, frmMain.dbConnect);
                    SqlDataAdapter datapter01 = new SqlDataAdapter(cmd01);
                    DataSet ds01 = new DataSet();
                    datapter01.Fill(ds01);
                    for (int j = 0; j < ds01.Tables[0].Rows.Count; j++)
                    {
                        TableLayoutPanel tabTmp02 = new TableLayoutPanel();
                        tabTmp02.Name = "tabTmp02";
                        tabTmp02.Tag = ds01.Tables[0].Rows[j]["ipaddr"].ToString();
                        tabTmp02.ColumnCount = 3;
                        tabTmp02.Margin = new System.Windows.Forms.Padding(1);
                        tabTmp02.Padding = new System.Windows.Forms.Padding(1);
                        //tabTmp02.BackColor = System.Drawing.Color.Red;
                        Size m02 = new Size();
                        m02.Width = 80;
                        m02.Height = 20;
                        tabTmp02.Size = m02;

                        TableLayoutPanel panel = new TableLayoutPanel();
                        panel.Anchor = AnchorStyles.Top;
                        panel.Name = "tabRows";
                        panel.ColumnCount = 3;
                        panel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 20F));
                        panel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 60F));
                        panel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F));
                        panel.Size = m02;
                        panel.Margin = new System.Windows.Forms.Padding(1);
                        panel.Padding = new System.Windows.Forms.Padding(1);



                        PictureBox picTmp = new PictureBox();
                        picTmp.Image = WFFSClients.Properties.Resources.T_GREEN;
                        picTmp.SizeMode = PictureBoxSizeMode.Normal;
                        picTmp.Padding = new System.Windows.Forms.Padding(0);
                        picTmp.Margin = new System.Windows.Forms.Padding(0);
                        Size picSize = new Size();
                        picSize.Width = 18;
                        picSize.Height = 18;
                        picTmp.Size = picSize;
                        picTmp.Name = "pic_User_" + ds01.Tables[0].Rows[j]["ipaddr"].ToString();
                        panel.Controls.Add(picTmp);

                        LinkLabel linkTmp = new LinkLabel();
                        linkTmp.Text = ds01.Tables[0].Rows[j]["UserName"].ToString();
                        linkTmp.LinkColor = Color.Black;
                        linkTmp.Name = "link_User";
                        linkTmp.Tag = ds01.Tables[0].Rows[j]["isLeader"].ToString() + "|" + ds01.Tables[0].Rows[j]["ipaddr"].ToString();
                        linkTmp.Click += new EventHandler(BtnPerToPer_Click);
                        panel.Controls.Add(linkTmp);

                        CheckBox ckUser = new CheckBox();
                        ckUser.Text = "";
                        ckUser.Padding = new System.Windows.Forms.Padding(0);
                        ckUser.Margin = new System.Windows.Forms.Padding(0);
                        ckUser.Checked = false;
                        ckUser.Name = "ck_User";
                        ckUser.Visible = false;
                        ckUser.Tag = ds01.Tables[0].Rows[j]["isLeader"].ToString() + "|" + ds01.Tables[0].Rows[j]["ipaddr"].ToString();
                        panel.Controls.Add(ckUser);

                        tabTmp02.Controls.Add(panel);
                        tabTmp01.Controls.Add(tabTmp02);

                    }

                    tabTmp.Controls.Add(tabTmp01, 2, i);

                    panelMain.Controls.Add(tabTmp);
                    

                    
                }
            }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            Control[] controls = panelMain.Controls.Find("ck_User", true);
            foreach (Control con in controls)
            {
                if (con is CheckBox)
                {
                    ((CheckBox)con).Checked = true;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Control[] controls = panelMain.Controls.Find("ck_User", true);
            foreach (Control con in controls)
            {
                if (con is CheckBox)
                {
                    ((CheckBox)con).Checked = false;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Control[] controls = panelMain.Controls.Find("ck_User", true);
            foreach (Control con in controls)
            {
                if (con is CheckBox)
                {
                    CheckBox tmpBox = (CheckBox)con;
                    if (tmpBox.Tag.ToString().Split('|')[0] == "1")
                    {
                        tmpBox.Checked = true;
                    }
                    else
                    {
                        tmpBox.Checked = false;
                    }
                }
            }
        }

        private void FrmSound_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmMain.frmSound = null;
        }



        #region 点对点通话
        public int CallType = 0;
        public string FlickerIp = String.Empty;
        /// <summary>
        /// 初始UDP通话
        /// </summary>
        private void InitUdpCall()
        {
        }


        public void CloseCall()
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                //this.button2.Text = "呼叫";
                //this.button3.Text = "取消";
                //this.button3.Visible = false;
                //this.CallType = 0;
                //去除闪动
                if (m_TimerDrawProgressBar != null)
                {
                    m_TimerDrawProgressBar.Dispose();
                    m_TimerDrawProgressBar = null;
                }
                ShowOtherCallBtn();
                frmMain.CloseWaveIn();
            }
            ));
        }

        public void SoundFlickerStart(string ip, int flickerType)
        {
            FlickerIp = ip;
            this.Invoke(new MethodInvoker(delegate()
            {
                if (m_TimerDrawProgressBar != null)
                {
                    m_TimerDrawProgressBar.Dispose();
                    m_TimerDrawProgressBar = null;
                }
                m_TimerDrawProgressBar = new System.Windows.Forms.Timer();
                if (flickerType == 0)
                {
                    m_TimerDrawProgressBar.Tick += new EventHandler(OnTimerChangeClientPictureBars);
                }
                else
                {
                    m_TimerDrawProgressBar.Tick += new EventHandler(OnTimerChangeServerPictureBars);
                }
                m_TimerDrawProgressBar.Interval = 100;
                m_TimerDrawProgressBar.Start();
            }));
        }

        public void SoundFickerYellow(string ip, int flickerType)
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                if (m_TimerDrawProgressBar != null)
                {
                    m_TimerDrawProgressBar.Dispose();
                    m_TimerDrawProgressBar = null;
                }
                if (flickerType == 0)
                {
                    Control[] controls = panelMain.Controls.Find("pic_User_" + ip, true);
                    if (controls.Length > 0)
                    {
                        PictureBox tmpPic = (PictureBox)controls[0];
                        tmpPic.Image = WFFSClients.Properties.Resources.T_YELLOW;
                    }
                }
                else
                {
                    picCenter.Image = WFFSClients.Properties.Resources.T_YELLOW;
                }
            }));
        }



        public void SoundFickerGreen(string ip, int flickerType)
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                if (m_TimerDrawProgressBar != null)
                {
                    m_TimerDrawProgressBar.Dispose();
                    m_TimerDrawProgressBar = null;
                }
                if (flickerType == 0)
                {
                    Control[] controls = panelMain.Controls.Find("pic_User_" + ip, true);
                    if (controls.Length > 0)
                    {
                        PictureBox tmpPic = (PictureBox)controls[0];
                        tmpPic.Image = WFFSClients.Properties.Resources.T_GREEN;
                    }
                }
                else
                {
                    picCenter.Image = WFFSClients.Properties.Resources.T_GREEN;
                }
            }));
        }

        public void SoundFickerToGreen(string ip, int flickerType)
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                if (flickerType == 0)
                {
                    Control[] controls = panelMain.Controls.Find("pic_User_" + ip, true);
                    if (controls.Length > 0)
                    {
                        PictureBox tmpPic = (PictureBox)controls[0];
                        tmpPic.Image = WFFSClients.Properties.Resources.T_GREEN;
                    }
                }
                else
                {
                    picCenter.Image = WFFSClients.Properties.Resources.T_GREEN;
                }
            }));
        }

        public void SoundFickerToRed(string ip, int flickerType)
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                Control[] controls = panelMain.Controls.Find("pic_User_" + ip, true);
                if (flickerType == 0)
                {
                    if (controls.Length > 0)
                    {
                        PictureBox tmpPic = (PictureBox)controls[0];
                        tmpPic.Image = WFFSClients.Properties.Resources.T_RED;
                        tmpPic.Image.Tag = "1";
                    }
                }
                else
                {
                    picCenter.Image = WFFSClients.Properties.Resources.T_RED;
                    picCenter.Image.Tag = "1";
                }
            }));
        }



        private void OnTimerChangeClientPictureBars(Object obj, EventArgs e)
        {
            Control[] controls = panelMain.Controls.Find("pic_User_" + FlickerIp, true);
            if (controls.Length > 0)
            {
                PictureBox tmpPic = (PictureBox)controls[0];
                if (this.m_TimerDrawProgressBar.Interval % 2 == 0)
                {
                    tmpPic.Image = WFFSClients.Properties.Resources.T_RED;
                }
                else
                {
                    tmpPic.Image = WFFSClients.Properties.Resources.T_YELLOW;
                }
                if (m_TimerDrawProgressBar.Interval == 150)
                {
                    m_TimerDrawProgressBar.Interval = 100;
                }
                m_TimerDrawProgressBar.Interval++;
            }
        }


        private void OnTimerChangeServerPictureBars(Object obj, EventArgs e)
        {
            if (this.m_TimerDrawProgressBar.Interval % 2 == 0)
            {
                picCenter.Image = WFFSClients.Properties.Resources.T_RED;
            }
            else
            {
                picCenter.Image = WFFSClients.Properties.Resources.T_YELLOW;
            }
            if (m_TimerDrawProgressBar.Interval == 150)
            {
                m_TimerDrawProgressBar.Interval = 100;
            }
            m_TimerDrawProgressBar.Interval++;
        }


        private void ChangeText()
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                if (m_TimerDrawProgressBar != null)
                {
                    m_TimerDrawProgressBar.Dispose();
                    m_TimerDrawProgressBar = null;
                }
                m_TimerDrawProgressBar = new System.Windows.Forms.Timer();
                m_TimerDrawProgressBar.Tick += new EventHandler(OnTimerDrawServerClientsPictureBars);
                m_TimerDrawProgressBar.Interval = 100;
                m_TimerDrawProgressBar.Start();
            }));
        }

        private void OnTimerDrawServerClientsPictureBars(Object obj, EventArgs e)
        {
            Control[] controls = panelMain.Controls.Find("pic_User_" + frmMain.UseringIP, true);
            if (controls.Length > 0)
            {
                PictureBox tmpPic = (PictureBox)controls[0];
                if (this.m_TimerDrawProgressBar.Interval % 2 == 0)
                {
                    tmpPic.Image = WFFSClients.Properties.Resources.T_RED;
                }
                else
                {
                    tmpPic.Image = WFFSClients.Properties.Resources.T_YELLOW;
                }
                m_TimerDrawProgressBar.Interval++;
            }
        }

        #region 通讯的交互控制
        public void ShowBroadcastingBtn()
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                Control[] TbPanel = panelMain.Controls.Find("tabTmp02", true);
                for (int i = 0; i < TbPanel.Length; i++)
                {
                    if (TbPanel[i] is TableLayoutPanel)
                    {
                        TableLayoutPanel tabTmp = (TableLayoutPanel)TbPanel[i];
                        tabTmp.Enabled = true;
                    }
                }
            }));
        }
        public void HideBroadcastingBtn()
        {
            //this.Invoke(new MethodInvoker(delegate()
            //{
                Control[] TbPanel = panelMain.Controls.Find("tabTmp02", true);
                //TableLayoutPanel TbPanl = (TableLayoutPanel)this.tabSL.Controls.Find("tabSLUsers", true)[0];

                for (int i = 0; i < TbPanel.Length; i++)
                {
                    if (TbPanel[i] is TableLayoutPanel)
                    {
                        TableLayoutPanel tabTmp = (TableLayoutPanel)TbPanel[i];
                        tabTmp.Enabled = false;
                    }
                }
            //}));
        }
        public void ShowSingleCallBtn(string ip)
        {
            Control[] controls = panelMain.Controls.Find("pic_User_" + ip, true);
            if (controls.Length > 0)
            {
                PictureBox tmpPic = (PictureBox)controls[0];
                Control tmpControl = tmpPic.Parent.Parent;
                tmpControl.Enabled = true;
            }
        }

        public void HideSingleCallBtn(string ip)
        {
            Control[] controls = panelMain.Controls.Find("pic_User_" + ip, true);
            if (controls.Length > 0)
            {
                PictureBox tmpPic = (PictureBox)controls[0];
                Control tmpControl = tmpPic.Parent.Parent;
                tmpControl.Enabled = false;
            }
        }
        public void HideOtherCallBtn(string ip)
        {
            this.Invoke(new MethodInvoker(delegate()
            {
                if (ip != Form1.ServicesCenterIP)
                {
                    panelCenter.Enabled = false;
                }
                Control[] TbPanel = panelMain.Controls.Find("tabTmp02", true);
                //TableLayoutPanel TbPanl = (TableLayoutPanel)this.tabSL.Controls.Find("tabSLUsers", true)[0];

                for (int i = 0; i < TbPanel.Length; i++)
                {
                    if (TbPanel[i] is TableLayoutPanel)
                    {
                        TableLayoutPanel tabTmp = (TableLayoutPanel)TbPanel[i];
                        if (!tabTmp.Tag.ToString().Contains(ip))
                        {
                            tabTmp.Enabled = false;
                        }
                    }
                }
            }));
        }
        public void ShowOtherCallBtn()
        {
            panelCenter.Enabled = true;
            //TableLayoutPanel TbPanl = (TableLayoutPanel)this.tabSL.Controls.Find("tabSLUsers", true)[0];
            Control[] TbPanel = panelMain.Controls.Find("tabTmp02", true);
            for (int i = 0; i < TbPanel.Length; i++)
            {
                if (TbPanel[i] is TableLayoutPanel)
                {
                    TableLayoutPanel tabTmp = (TableLayoutPanel)TbPanel[i];
                    tabTmp.Enabled = true;
                }
            }
        }
        public bool VaildIpIsUse(string ip)
        {
            bool reValue = false;
            Control[] controls = panelMain.Controls.Find("pic_User_" + ip, true);
            if (controls.Length > 0)
            {
                PictureBox tmpPic = (PictureBox)controls[0];
                if (tmpPic.Image.Tag != null && tmpPic.Image.Tag.ToString() == "1")
                {
                    reValue = true;
                }
            }
            return reValue;
        }
        #region 暂时屏蔽代码
        //public bool BeginCallIp(object sender, string targetIP)
        //{
        //    LinkLabel btnCaller = (LinkLabel)sender;
        //    bool reValue = true;

        //    //改变字体，让它进入等待状态

        //    //向目标机器发送消息，如果不成功，那么则中断字体显示
        //    if (VaildIP(targetIP))
        //    {
        //        HideOtherCallBtn(targetIP);
        //        ChangeText();
        //        //btnCaller.Text = "等待中......";
        //        frmMain.aduio_client_netWork = new NetworkManager
        //                                 (
        //                                 targetIP, Form1.Audio_serverPort,
        //                               SOKET_TYPE.enClientMode
        //                                 );
        //        frmMain.aduio_client_netWork.ConnectToListeningServer();

        //        msgAudioCall = new WBMsgAudioCall();
        //        msgAudioCall.callType = 1;
        //        msgAudioCall.TargetIP = Form1.Audio_serverIP;
        //        frmMain.aduio_client_netWork.SendWBMsgConnectedHdlr(msgAudioCall);
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //    try
        //    {
        //        frmMain.m_pTargetEP = new IPEndPoint(IPAddress.Parse(targetIP), Convert.ToInt32(Form1.Udp_AudioPort));
        //    }
        //    catch
        //    {
        //        MessageBox.Show(this, "Invalid target IP address or port !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        return false;
        //    }
        //    //呼叫端需要将标志设置为等待回连后的状态,同时如果在该状态点击按钮，那么将关闭通话
        //    CallType = 2;
        //    return true;
        //}
        ///// <summary>
        ///// 用来处理接收到呼叫端的信息
        ///// </summary>
        ///// <param name="CallerIP">呼叫端的IP</param>
        //public void WaitForCall(string CallerIP)
        //{
        //    this.Invoke(new MethodInvoker(delegate()
        //    {
        //        //    //TableLayoutPanel TbPanl = (TableLayoutPanel)this.tabSL.Controls.Find("tabSLUsers", true)[0];
        //        //    TableLayoutPanel TbPanl = new TableLayoutPanel();
        //        //    for (int i = 0; i < TbPanl.Controls.Count; i++)
        //        //    {
        //        //        Button btnTmp = (Button)TbPanl.Controls[i];
        //        //        if (!btnTmp.Name.Contains(CallerIP) && !btnTmp.Name.Contains("CallOut"))
        //        //        {
        //        //            btnTmp.Enabled = true;
        //        //        }
        //        //        if (!btnTmp.Name.Contains(CallerIP) && !btnTmp.Name.Contains("CallIn"))
        //        //        {
        //        //            btnTmp.Enabled = true;
        //        //            btnTmp.Text = "接听";
        //        //        }
        //        //    }
        //        //    //this.button2.Text = "接听";
        //        //    //this.button3.Text = "取消";
        //        //    //this.button3.Visible = true;
        //        //    this.CallerIP = CallerIP;
        //        //    //被呼叫端需要将状态设置为等待接听状态
        //        //    this.CallType = 1;
        //        //    if (m_TimerDrawProgressBar != null)
        //        //    {
        //        //        m_TimerDrawProgressBar.Dispose();
        //        //        m_TimerDrawProgressBar = null;
        //        //    }
        //        //    m_TimerDrawProgressBar = new System.Windows.Forms.Timer();
        //        //    //m_TimerDrawProgressBar.Tick += new EventHandler(OnTimerDrawServerClientsProgressBars);
        //        //    m_TimerDrawProgressBar.Interval = 100;
        //        //    m_TimerDrawProgressBar.Start();
        //        //    try
        //        //    {
        //        //        //被呼叫端与呼叫端进行UDP连接
        //        //        frmMain.m_pTargetEP = new IPEndPoint(IPAddress.Parse(this.CallerIP), Convert.ToInt32(Form1.Udp_AudioPort));
        //        //    }
        //        //    catch
        //        //    {
        //        //        MessageBox.Show(this, "Invalid target IP address or port !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        //        return;
        //        //    }
        //        //}
        //        HideOtherCallBtn(CallerIP);
        //        frmMain.UseringIP = CallerIP;
        //        frmMain.CallerIP = CallerIP;
        //        this.CallType = 1;
        //        ChangeText();
        //        try
        //        {
        //            //被呼叫端与呼叫端进行UDP连接
        //            frmMain.m_pTargetEP = new IPEndPoint(IPAddress.Parse(frmMain.CallerIP), Convert.ToInt32(Form1.Udp_AudioPort));
        //        }
        //        catch
        //        {
        //            MessageBox.Show(this, "Invalid target IP address or port !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //            return;
        //        }
        //    }));
        //}

        //public void BCall()
        //{
        //    this.Invoke(new MethodInvoker(delegate()
        //    {
        //        //TableLayoutPanel TbPanl = (TableLayoutPanel)this.tabSL.Controls.Find("tabSLUsers", true)[0];
        //        //TableLayoutPanel TbPanl = new TableLayoutPanel();
        //        //for (int i = 0; i < TbPanl.Controls.Count; i++)
        //        //{
        //        //    Button btnTmp = (Button)TbPanl.Controls[i];
        //        //    if (!btnTmp.Name.Contains(CallerIP) && !btnTmp.Name.Contains("CallOut"))
        //        //    {
        //        //        btnTmp.Enabled = false;
        //        //    }
        //        //    if (!btnTmp.Name.Contains(CallerIP) && !btnTmp.Name.Contains("CallIn"))
        //        //    {
        //        //        btnTmp.Enabled = true;
        //        //        btnTmp.Text = "断开通话";
        //        //    }
        //        //}
        //        //去除闪动
        //        if (m_TimerDrawProgressBar != null)
        //        {
        //            m_TimerDrawProgressBar.Dispose();
        //            m_TimerDrawProgressBar = null;
        //        }
        //        //将提示图片改成黄色表示已经连接
        //        Control[] controls = panelMain.Controls.Find("pic_User_" + frmMain.CallerIP, true);
        //        if (controls.Length > 0)
        //        {
        //            PictureBox tmpPic = (PictureBox)controls[0];
        //            tmpPic.Image = WFFSClients.Properties.Resources.T_YELLOW;
        //        }
        //        this.CallType = 2;
        //        frmMain.m_IsSendingMic = true;

        //        frmMain.m_pWaveIn = new WaveIn(WaveIn.Devices[Form1.WavInDevice_idx], 8000, 16, 1, 400);
        //        frmMain.m_pWaveIn.BufferFull += new BufferFullHandler(frmMain.m_pWaveIn_BufferFull);
        //        frmMain.m_pWaveIn.Start();

        //    }
        //    ));
        //}
        #endregion

        #endregion






        private void BtnPerToPer_Click(object sender, EventArgs e)
        {
            string controlSql = String.Empty;
            SqlCommand command = null;
            SqlDataAdapter adapter = null;
            LinkLabel btnCaller = (LinkLabel)sender;
            string callerIp = btnCaller.Tag.ToString().Split('|')[1];
            if (VaildIpIsUse(callerIp))
            {
                return;
            }
            //呼叫方
            DataTable dtCaller = new DataTable();
            //自身
            DataTable dtSelf = new DataTable();
            controlSql = "SELECT ipaddr,isLeader,isHasPad,IsBroadcasting,IsCall,callIp,IsBroadcastingHandle,IsUdpCallHandle from UserInfo where ipaddr = '" + callerIp + "'";
            command = new SqlCommand(controlSql, frmMain.dbConnect);
            adapter = new SqlDataAdapter(command);
            adapter.Fill(dtCaller);
            controlSql = "SELECT ipaddr,isLeader,isHasPad,IsBroadcasting,IsCall,callIp,IsBroadcastingHandle,IsUdpCallHandle from UserInfo where ipaddr = '" + Form1.Audio_serverIP + "'";
            command = new SqlCommand(controlSql, frmMain.dbConnect);
            adapter = new SqlDataAdapter(command);
            adapter.Fill(dtSelf);

            if (dtSelf.Rows.Count > 0 && dtCaller.Rows.Count > 0)
            {
                string selfCallState = dtSelf.Rows[0]["IsCall"].ToString();
                string callerCallState = dtCaller.Rows[0]["IsCall"].ToString();

                //如果双方都没有开始呼叫过
                if (selfCallState == "0" && callerCallState == "0")
                {
                    //判断该IP是否处于空闲
                    controlSql = "SELECT IsCall from UserInfo where ipaddr = '" + callerIp + "'";
                    command = new SqlCommand(controlSql, frmMain.dbConnect);
                    object tmpObj = command.ExecuteScalar();
                    if (tmpObj != null && tmpObj.ToString() == "0")
                    {
                        //将对应IP状态设置成为主动等待连接101
                        controlSql = "UPDATE UserInfo SET IsCall=102,callIp='" + callerIp + "' where ipaddr = '" + Form1.Audio_serverIP + "'";
                        command = new SqlCommand(controlSql, frmMain.dbConnect);
                        command.ExecuteNonQuery();
                        controlSql = "UPDATE UserInfo SET IsCall=202,callIp='" + Form1.Audio_serverIP + "' where ipaddr = '" + callerIp + "'";
                        command = new SqlCommand(controlSql, frmMain.dbConnect);
                        command.ExecuteNonQuery();
                        HideOtherCallBtn(callerIp);
                    }
                }

                if (selfCallState == "102")
                {
                    if (MessageBox.Show("是否确认中断通话等待状态？", "中断通话等待状态提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        //将对应IP状态设置成为主动等待连接101
                        controlSql = "UPDATE UserInfo SET IsCall=103,IsUdpCallHandle=0 where ipaddr = '" + Form1.Audio_serverIP + "'";
                        command = new SqlCommand(controlSql, frmMain.dbConnect);
                        command.ExecuteNonQuery();
                        controlSql = "UPDATE UserInfo SET IsCall=103,IsUdpCallHandle=0 where ipaddr = '" + callerIp + "'";
                        command = new SqlCommand(controlSql, frmMain.dbConnect);
                        command.ExecuteNonQuery();
                    }
                }

                if (selfCallState == "202")
                {
                    if (MessageBox.Show("是否进行通话?点击【是】连接通话,点击【否】中断通话!", "通话连接提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        //将对应IP状态设置成为被动等待连接101
                        controlSql = "UPDATE UserInfo SET IsCall=201,IsUdpCallHandle=0 where ipaddr = '" + Form1.Audio_serverIP + "'";
                        command = new SqlCommand(controlSql, frmMain.dbConnect);
                        command.ExecuteNonQuery();
                        controlSql = "UPDATE UserInfo SET IsCall=101,IsUdpCallHandle=0 where ipaddr = '" + callerIp + "'";
                        command = new SqlCommand(controlSql, frmMain.dbConnect);
                        command.ExecuteNonQuery();
                    }
                    else
                    {
                        //将对应IP状态设置成为中断状态
                        controlSql = "UPDATE UserInfo SET IsCall=103,IsUdpCallHandle=0 where ipaddr = '" + Form1.Audio_serverIP + "'";
                        command = new SqlCommand(controlSql, frmMain.dbConnect);
                        command.ExecuteNonQuery();
                        controlSql = "UPDATE UserInfo SET IsCall=103,IsUdpCallHandle=0 where ipaddr = '" + callerIp + "'";
                        command = new SqlCommand(controlSql, frmMain.dbConnect);
                        command.ExecuteNonQuery();
                    }
                }

                if (selfCallState == "101")
                {
                    if (MessageBox.Show("是否确认中断通话？", "中断通话提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        //将对应IP状态设置成为主动等待连接103
                        controlSql = "UPDATE UserInfo SET IsCall=103,IsUdpCallHandle=0 where ipaddr = '" + Form1.Audio_serverIP + "'";
                        command = new SqlCommand(controlSql, frmMain.dbConnect);
                        command.ExecuteNonQuery();
                        controlSql = "UPDATE UserInfo SET IsCall=203,IsUdpCallHandle=0 where ipaddr = '" + callerIp + "'";
                        command = new SqlCommand(controlSql, frmMain.dbConnect);
                        command.ExecuteNonQuery();
                    }
                }

                if (selfCallState == "201")
                {
                    if (MessageBox.Show("是否确认中断通话？", "中断通话提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        //将对应IP状态设置成为主动等待连接101
                        controlSql = "UPDATE UserInfo SET IsCall=203,IsUdpCallHandle=0 where ipaddr = '" + Form1.Audio_serverIP + "'";
                        command = new SqlCommand(controlSql, frmMain.dbConnect);
                        command.ExecuteNonQuery();
                        controlSql = "UPDATE UserInfo SET IsCall=103,IsUdpCallHandle=0 where ipaddr = '" + callerIp + "'";
                        command = new SqlCommand(controlSql, frmMain.dbConnect);
                        command.ExecuteNonQuery();
                    }
                }
            }
        }

        #endregion

        private void FrmSound_FormClosed(object sender, FormClosedEventArgs e)
        {
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmSound", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmSound", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }
        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    }
}
